// pages/yuyue/yuyue.js
// directory.js
var app = getApp()

var animation
Page({
  /**
   * 页面的初始数据
   * 当前    provinces:所有省份
   * citys选择省对应的所有市,
   * areas选择市对应的所有区
   * provinces：当前被选中的省
   * city当前被选中的市
   * areas当前被选中的区
   */
  data: {
    xsks: 0,
    totalPrice: "1370.00",           // 总价，初始为0
    menuType: 0,
    begin: null,
    status: 1,
    end: null,
    isVisible: false,
    animationData: {},
    animationAddressMenu: {},
    addressMenuIsShow: false,
    value: [0, 0, 0, 0],
    items: [
      { name: 'USA', value: '线上支付', checked: 'true' },
      { name: 'CHN', value: '线下支付' },
    ],
    // provinces: [],
    // citys: [],
    // areas: [],
    // province: '',
    // city: '',
    // area: '',
    // date: '2017-08-01',
    // showtime1: true,
    // showtime2: false,
    // reg_hqyzm: "获取验证码",
    // countdown_time: 60,
    allchose: [
      { chose: '移动宽带' },
      { chose: '联通宽带' },
      { chose: '电信宽带' },

    ]
  },
  yuyue: function () {
    app.navTo('../order/order')
  },
  //宽带类型选择
  zhchange: function (e) {
    var id = e.currentTarget.dataset.id;  //获取自定义的ID值  
    console.log(id)
    var xianshiks = this.data.xsks; //xianshi
    var chose = this.data.allchose;
    var kss = chose[id].chose;
    xianshiks = kss;
    this.setData({
      xsks: xianshiks,
      id: id
    })
  },
  // submit: function (e) {
  //   var that = this;
  //   var phone = e.detail.value.phone;
  //   var uname = e.detail.value.uname;
  //   var ads = e.detail.value.ads;
  //   var youbian = e.detail.value.youbian;
  //   if (uname == "" || phone == "" || ads == "" || nr == "") {
  //     wx.showToast({
  //       title: '不能为空',
  //       icon: 'none',
  //       duration: 1500
  //     })
  //     return false;
  //   } else if (!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(phone))) {
  //     wx.showToast({
  //       title: '手机号有误！',
  //       icon: 'none',
  //       duration: 1500
  //     })
  //     return false;
  //   }
  //   else {
  //     wx.showToast({
  //       title: '提交成功',
  //       icon: 'success',
  //       duration: 1500
  //     })
  //     setTimeout(function () {				//确认之后跳转的页面
  //       wx.navigateTo({
  //         url: '../ads/ads',
  //       })
  //     }, 1500);
  //   }
  // },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    wx.request({
      url: app.appUrl + '/goods/detail',
      data: {
        goodsid: options.id
      },
      success:function(res){
        that.setData({
          goods: res.data.data
        })
      }
    })
  },
  hideMenuTap: function (e) {
    this.startAnimation(false, -200)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  formSubmit:function(e){
   
    var that=this;
    var phone = e.detail.value.phone ;
    var uname = e.detail.value.uname;
    var adds = e.detail.value.ads;
    var remark = e.detail.value.nr;
    if (phone == '' || uname == '' || adds == '' ){
      wx.showToast({
        title: '内容不完整',
        icon:'none',
        duration:1000
      })
      return false;
    }

    wx.request({
      url: app.appUrl + '/order/insertOrder',
      data:{
        uid: wx.getStorageSync('userinfo').uid,
        goods_data: {
          remark: remark ? remark : '',
          goodsid: that.data.goods.goodsid,
          number: 1
        },
        adds:{
          name: uname,
          phone:phone,
          address: adds
        }
      },
      method:'post',
     success:function(rs){
       if (!that.data.radio || that.data.radio=='USA'){
         that.goodspay(rs);
       }else{
         wx.showModal({
           title: '提示',
           content: '确定通过线下人员交易',
           success:function(res){
              if(res.confirm==1){
                wx.request({
                  url: app.appUrl +'/order/trade',
                  data:{
                    uid: wx.getStorageSync('userinfo').uid,
                    order_no: rs.data.data.order_no,
                  },
                  method:'post',
                  success:function(e){
                   if(e.data.code==1){
                     wx.showToast({
                       title: e.data.msg,
                       success:function(){
                         wx.navigateTo({
                           url: '/pages/myorder/myorder',
                         })
                       }
                     })
                   }else{
                     wx.showToast({
                       title: e.data.msg,
                       icon:'none'
                     })
                   }
                  }
                })
              }
           }
         })
       }
      
       }
      
    })
  },
  goodspay: function (rs) {
    wx.showLoading({
      title: '请稍后',
    })
    if (rs.data.code == 1) {
      wx.request({
        url: app.appUrl + '/order/orderPayParams',
        data: {
          order_no: rs.data.data.order_no,
          uid: wx.getStorageSync('userinfo').uid,
          title: '商品支付'
        },
        method: 'post',
        success: function (e) {
          if (e.data.code == 1) {
            wx.requestPayment({
              timeStamp: e.data.data.weixin.timeStamp,
              nonceStr: e.data.data.weixin.nonceStr,
              package: e.data.data.weixin.package,
              signType: e.data.data.weixin.signType,
              paySign: e.data.data.weixin.paySign,
              success: function () {
                wx.navigateTo({
                  url: '/pages/myorder/myorder',
                })
                wx.hideLoading();
              },
              fail: function () {
                wx.hideLoading();
                wx.showToast({
                  title: '支付失败',
                  icon: 'none'
                })
              }
            })
          } else {
            wx.hideLoading();
            wx.showToast({
              title: '失败',
              icon: 'none'
            })
          }
        }
      })

    } else {
      wx.hideLoading();
      wx.showToast({
        title: '失败',
        icon: 'none'
      })
    }
  },
  //单选
  radioChange:function(e){
    this.setData({
      radio: e.detail.value
    })
    console.log(e.detail.value)
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      hasList: true,        // 既然有数据了，那设为true吧
      carts: [
        { id: 1, title: '【需使用4G飞享套餐。赠送12GB流量，2400分钟通话', image: '../../images/banner2.jpg', num: 1, price: 13.00, selected: true }
      ]
    });
    // console.log(this.carts.length)
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 点击所在地区弹出选择框
  selectDistrict: function (e) {
    var that = this
    console.log('111111111')
    if (that.data.addressMenuIsShow) {
      return
    }
    that.startAddressAnimation(true)
  },
  // 执行动画
  startAddressAnimation: function (isShow) {
    console.log(isShow)
    var that = this
    if (isShow) {
      that.animation.translateY(0 + 'vh').step()
    } else {
      that.animation.translateY(40 + 'vh').step()
    }
    that.setData({
      animationAddressMenu: that.animation.export(),
      addressMenuIsShow: isShow,
    })
  },
  // 点击地区选择取消按钮
  cityCancel: function (e) {
    this.startAddressAnimation(false)
  },
  // 点击地区选择确定按钮
  citySure: function (e) {
    var that = this
    var city = that.data.city
    var value = that.data.value
    that.startAddressAnimation(false)
    // 将选择的城市信息显示到输入框
    var areaInfo = that.data.provinces[value[0]].name + ',' + that.data.citys[value[1]].name + ',' + that.data.areas[value[2]].name
    that.setData({
      areaInfo: areaInfo,
    })
  },
  hideCitySelected: function (e) {
    console.log(e)
    this.startAddressAnimation(false)
  },
  onShareAppMessage: function () {

  },
  reg_yanzhengma_huoqu: function () {
    console.log("004");
    this.setData({
      showtime1: false,
      showtime2: true,

    });
    countdown(this);
    // wxTimer.start(this);  
  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },
  bindTimeChange: function (e) {
    this.setData({
      time: e.detail.value
    })
  }

});
function countdown(that) {
  var second = that.data.countdown_time
  if (second == 0) {
    console.log("001");
    // console.log("Time Out...");  
    that.setData({
      // second: "Time Out..."  

      showtime1: true,
      showtime2: false,
      reg_hqyzm: "重新获取",
      countdown_time: 60,
    });
    console.log("002");
    return;
  }
  console.log("003");
  var time = setTimeout(function () {
    that.setData({
      countdown_time: second - 1
    });
    countdown(that);
  }
    , 1000)
}  